/**
 * 
 */
package com.gkwave.action.service;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gkwave.module.bean.GymYogaBean;
import com.gkwave.module.dao.GymYogaDAO;

/**
 * @author Administrator
 *
 */
public class HealthCareGymYogaService extends BaseService {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3522800963237359381L;

	/**
	 * 
	 */
	public HealthCareGymYogaService() {
		// TODO Auto-generated constructor stub
	}

	/* (non-Javadoc)
	 * @see com.gkwave.action.service.BaseService#procesRequest(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	public void procesRequest(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		/*
		 * idType (1 - Gym ;2 - Yoga)
		*/ 
		String idType = request.getParameter("idType");
		Integer id_type = 0;
		GymYogaBean bean = null;
		try{
			if(idType!=null){
				id_type = Integer.parseInt(idType);
				String query1 = null;
				String query2 = null;
				if(id_type == 1){
					// Gym
					query1 = "SELECT t1.id_gym AS id,t1.nm_gym AS name,t2.nm_branch,t1.id_email,t1.nr_total_seat,t1.nr_fee,t1.nm_slot_time1,t1.nm_slot_time2 FROM m_gym t1,m_branch t2 WHERE t1.id_branch=t2.id_branch";
				} else if(id_type==2){
					// Yoga
					query1 = "SELECT t1.id_yoga AS id,t1.nm_yoga AS name,t2.nm_branch,t1.id_email,t1.nr_total_seat,t1.nr_fee,t1.nm_slot_time1,t1.nm_slot_time2 FROM m_yoga t1,m_branch t2 WHERE t1.id_branch=t2.id_branch";
				} else{
					xmlResponse = "Invalid input, idType= " +idType;
				}
				GymYogaDAO dao = new GymYogaDAO();
				bean = dao.getResultList(query1);
				
				//
				List<GymYogaBean> list = bean.entries;
				Iterator<GymYogaBean> iterator = list.iterator();
				while(iterator.hasNext()){
					GymYogaBean object = (GymYogaBean) iterator.next();
					if(id_type==1){
						query1 = "SELECT COUNT(*) AS booked_seat FROM h_gym_booking h WHERE in_active='Y' and id_slot = 'M' and id_gym=" +object.getId();
						query2 = "SELECT COUNT(*) AS booked_seat FROM h_gym_booking h WHERE in_active='Y' and id_slot = 'E' and id_gym=" +object.getId();
					} else if(id_type==2){
						query1 = "SELECT COUNT(*) AS booked_seat FROM h_yoga_booking h WHERE in_active='Y' and id_slot = 'M' and id_yoga=" +object.getId();
						query2 = "SELECT COUNT(*) AS booked_seat FROM h_yoga_booking h WHERE in_active='Y' and id_slot = 'E' and id_yoga=" +object.getId();
					}
					Integer nrBookedSeat1 = dao.getTotalBookedSeat(0, query1);
					Integer nrBookedSeat2 = dao.getTotalBookedSeat(0, query2);
					Integer availableSeat_M = object.getNrTotalSeat() - nrBookedSeat1;
					Integer availableSeat_E = object.getNrTotalSeat() - nrBookedSeat2;
					object.setNrAvailableSeat_M(availableSeat_M);
					object.setNrAvailableSeat_E(availableSeat_E);
				}
				
				xmlResponse = bean.marsel(bean);
			} else{
				xmlResponse = "Input is empty, idType= " +idType;
			}
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			xmlResponse = "Exception, e = " +e.getMessage();
		}
		out = response.getWriter();
		System.out.println(xmlResponse);
		out.println(xmlResponse);
	}

}
